<?php

require_once('BaseModel.php');

class PrivilegiosSites extends BaseModel{
	protected $_name = "sites_privilegios";

	public function getByNivel( $id ){
		if($id){
			$select = $this->select()->where("sites_id = ?", $id);
			
			$data = $this->fetchAll( $select );
			if( $data )
				$data = $data->toArray();

			$privilegios = array();
			foreach( $data as $d ){
				$privilegios[] = $d['regra'];	
			}
			return $privilegios;
		}
		return NULL;
	}

	public function getByNivelWithModules( $id ){
		if($id){
			$select = $this->select()->where("sites_id = ?", $id);

			$data = $this->fetchAll( $select );
			if( $data )
				$data = $data->toArray();

			return $data;
		}
		return NULL;
	}
	
	public function getPrivelegiosByNivel( $sites_id, $nivel ){

		$query = "SELECT sp.* FROM sites_privilegios as sp
			LEFT JOIN sites as s ON (sp.sites_id = s.id )
			LEFT JOIN usuarios_privilegios as up ON (s.profile = up.nivel AND sp.regra = up.regra )
			WHERE sp.sites_id = '".$sites_id."'  AND up.nivel = '".$nivel."'
			GROUP BY regra";
		$data = $this->getDb()->query( $query );

		if($data)
			return $data->fetchAll();

	}
}